<template>
  <div id="app">
    <router-view v-if="isShow"></router-view>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import WebsocketComm from "@/utils/webSocket/webSocketComm";
export default {
  name: "App",
  data() {
    return {
      isShow: false,
    };
  },
  created() {
    // 获取语言类型
    this.langType = Cookies.get("lang") || "1";
    // 判断缓存是否存在用户id，有则存在vuex中
    if (localStorage.getItem("userId")) {
      this.$store.commit("setUserId", localStorage.getItem("userId"));
    }

    // 刷新页面
    if (this.$route.path !== "/" || this.$route.name != "siteMap") {
      this.$router.push({
        path: "/",
      });
    }
  },
  async mounted() {
    await this.initMap();
    // this.$store.commit("setLnglat", [38.75931070466901,9.011560457561558]);
    // sessionStorage.setItem("setLnglat", JSON.stringify([38.75931070466901,9.011560457561558]));
    // this.sendLngLat();
  },
  methods: {
    judgeCoordsInArea(dot, range) {
      console.log("当前定位", dot);
      console.log("范围", range);
      let count = 0,
        t1,
        t2,
        term,
        r1 = range[0],
        r2;
      for (let i = 1; i <= range.length; i++) {
        r2 = range[i % range.length];
        t1 =
          dot[0] > Math.min(r1[0], r2[0]) && dot[0] <= Math.max(r1[0], r2[0]);
        t2 = dot[1] <= Math.max(r1[1], r2[1]) && r1[0] !== r2[0];
        if (t1 && t2) {
          term = ((dot[0] - r1[0]) * (r2[1] - r1[1])) / (r2[0] - r1[0]) + r1[1];
          if (r1[1] === r2[1] || dot[1] <= term) count++;
        }
        r1 = r2;
      }
      return count % 2 !== 0;
    },
    initMap() {
      this.isShow = false;
      let that = this;
      $.ajax({
        type: "post",
        url: "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyBpf9DOqJd_e2z_2ZHZ4KUp4rc4mmOa45o",
        success: function (data) {
          console.log('当前经纬度',data);
          let dot = [data.location.lng, data.location.lat];
          let range = [
            [38.55406553260018, 9.181502621975557],
            [39.07155835657683, 9.181502621975557],
            [39.07155835657683, 8.733081165581707],
            [38.55406553260018, 8.733081165581707],
            [38.55406553260018, 9.181502621975557],
          ];
          // if (that.judgeCoordsInArea(dot, range)) {
            that.$store.commit("setLnglat", [
              data.location.lng,
              data.location.lat,
            ]);

            sessionStorage.setItem(
              "setLnglat",
              JSON.stringify([data.location.lng, data.location.lat])
            );
          // } else {
          //   // console.log("不在当前范围");
          //   that.$store.commit(
          //     "setLnglat",
          //     [38.75931070466901, 9.011560457561558]
          //   );
          //   sessionStorage.setItem(
          //     "setLnglat",
          //     JSON.stringify([38.75931070466901, 9.011560457561558])
          //   );
          // }
          that.sendLngLat();
        },
      });
    },
    // 将当前定位经纬度发送给ws
    sendLngLat() {
      new WebsocketComm({
        url: window.WEBSOCKET.WORKBENCH_BASE_API,
      });
      this.isShow = true;
    },
  },
};
</script>

<style>
::placeholder {
  font-size: 12px;
}
#app,
html,
body {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  height: 100%;
  overflow: hidden;
}
* {
  padding: 0;
  margin: 0;
}
li {
  list-style: none;
}

::webkit-scrollbar {
  display: none;
}
</style>
